$(function() {
	//页数 
	var page = 0;
	// 每页展示10个
	var size = 10;
	$('.neirongbox').dropload({
		scrollArea: window,
		autoLoad: true, //自动加载
		domDown: { //上拉
			domClass: 'dropload-down',
			domRefresh: '<div class="dropload-refresh f15 "><i class="icon icon-20"></i>上拉加载更多</div>',
			domLoad: '<div class="dropload-load f15"><span class="weui-loading"></span>正在加载中...</div>',
			domNoData: '<div class="dropload-noData">没有更多数据了</div>'
		},
		domUp: { //下拉
			domClass: 'dropload-up',
			domRefresh: '<div class="dropload-refresh"><i class="icon icon-114"></i>上拉加载更多</div>',
			domUpdate: '<div class="dropload-load f15"><i class="icon icon-20"></i>释放更新...</div>',
			domLoad: '<div class="dropload-load f15"><span class="weui-loading"></span>正在加载中...</div>'
		},
		loadUpFn: function(me) { //下拉刷新  
			$.ajax({
				type: 'GET',
				url: 'http://localhost/WaterGoods/index.php?s=/addon/IcWaterGoods/mobile/ajaxGetGoods/page/1/size/'+size,

				dataType: 'json',
				success: function(dataArray) {
					++page;
					var reslt = '';
					for(var i = 0; i < dataArray.length; i++) {
						var data = dataArray[i];
						reslt += '<div class="goods weui_cell moments__post weui-border-b">' +
								'<input type="hidden" value="'+ data.id +'" name="id" >' +
								'<div class="weui_cell_hd">' +
								'<img class="circle" src="'+data.head+'">' +
								'</div>' +
								'<div class="weui_cell_bd">' +
								'<a class="title" href="javascript:;">' +
								'<span class="f-blue">'+data.username+'</span>' +
								'</a>' +
								'<p id="paragraph" class="paragraph">' +
								data.desc +
								'</p>' +
								'<div class="thumbnails">'
								for(var j = 0 ; j < data.imgs.length ; ++j) {
                                    reslt += '<div class="roo weui-updown">' +
                                    '<img class="img-max lazyload" src="'+data.imgs[j]+'" style="-webkit-animation: fadeIn 01s ease 0.2s 1 both;animation: fadeIn 1s ease 0.2s 1 both;">' +
										'</div>';
								}

                        reslt +=
								'</div>' +
								'<div class="toolbar">' +
								'<p class="timestamp"><span>'+data.date+'</span></p>' +
								'<div>' +
								'<div id="actionMenu" class="actionMenu slideIn">' +
								'<p class="actionBtn" id="btnLike"><i class="icon icon-96"></i></p>' +
								'<p class="actionBtn" id="btnComment"><i class="icon icon-3"></i></p>' +
								'</div>' +
								'</div>' +
								'<span id="actionToggle" class="actionToggle">..</span>' +
								'</div>' +
								'<div class="liketext">';
								for(var j = 0 ; j < data.comment.length ; ++j) {
									var obj = data.comment[j];
									if(obj.receiver) {
										// 有接收人
										reslt += '<p><span class="nickname f-blue">'+obj.username+'</span>回复<span class="nickname  f-blue">'+obj.receiver+'</span>:<span class="nickname">'+obj.content+'</span></p>';
									}else {
                                        // 没有接收人
                                        reslt += '<p><span class="nickname f-blue">'+obj.username+'</span>:<span class="nickname">'+obj.content+'</span></p><br />';
                                    }
								}



								reslt += '</div>' +
								'<div class="weui_cells weui_cells_form" style="display: none;">' +
								'<div class="weui_cell weui_vcode">' +
								'<div class="weui_cell_bd weui_cell_primary">' +
								'<input class="weui_input" type="text" placeholder="评论">' +
								'</div>' +
								'<div class="weui_cell_ft">' +
								'<a href="javascript:;" class="weui-vcode-btn bg-orange">发送</a>' +
								'</div>' +
								'</div>' +
								'</div>' +
								'</div>' +
								'</div>';
					}
					// 为了测试，延迟1秒加载
					setTimeout(function() {
						$('.moments').html(reslt);
						
						// 每次数据加载完，必须重置
						me.resetload();
						// 重置索引值，重新拼接more.json数据
                        page++;
						// 解锁
						me.unlock();
						me.noData(false);
						// me.hasData();
					}, 1000);

				},
				error: function(xhr, type) {
					alert('Ajax error!');
					// 即使加载出错，也得重置
					me.resetload();
				}
			});
		},
		loadDownFn: function(me) { //上啦加载更多

			page++;
			window.history.pushState(null, document.title, window.location.href);
			var result = '';
			$.ajax({
				type: 'GET',
				url: 'http://localhost/WaterGoods/index.php?s=/addon/IcWaterGoods/mobile/ajaxGetGoods/page/'+page+'/size/'+size,
				dataType: 'json',
				success: function(dataArray) {
					var arrLen = dataArray.length;
					if(arrLen > 0) {

						for(var i = 0; i < arrLen; i++) {
                            var data = dataArray[i];
                            result += '<div class="article weui_cell moments__post weui-border-b">' +
                                '<input type="hidden" value="'+ data.id +'" name="id" >' +
                                '<div class="weui_cell_hd">' +
                                '<img class="circle" src="'+data.head+'">' +
                                '</div>' +
                                '<div class="weui_cell_bd">' +
                                '<a class="title" href="javascript:;">' +
                                '<span class="f-blue">'+data.username+'</span>' +
                                '</a>' +
                                '<p id="paragraph" class="paragraph">' +
                                data.desc +
                                '</p>' +
                                '<div class="thumbnails">'
                            for(var j = 0 ; j < data.imgs.length ; ++j) {
                                result += '<div class="roo weui-updown">' +
                                    '<img class="img-max lazyload" src="'+data.imgs[j]+'" style="-webkit-animation: fadeIn 01s ease 0.2s 1 both;animation: fadeIn 1s ease 0.2s 1 both;">' +
                                    '</div>';
                            }

                            result +=
                                '</div>' +
                                '<div class="toolbar">' +
                                '<p class="timestamp"><span>'+data.date+'</span></p>' +
                                '<div>' +
                                '<div id="actionMenu" class="actionMenu slideIn">' +
                                '<p class="actionBtn" id="btnLike"><i class="icon icon-96"></i></p>' +
                                '<p class="actionBtn" id="btnComment"><i class="icon icon-3"></i></p>' +
                                '</div>' +
                                '</div>' +
                                '<span id="actionToggle" class="actionToggle">..</span>' +
                                '</div>' +
                                '<div class="liketext">';
                            for(var j = 0 ; j < data.comment.length ; ++j) {
                                var obj = data.comment[j];
                                if(obj.receiver) {
                                    // 有接收人
                                    result += '<p><span class="nickname f-blue">'+obj.username+'</span>回复<span class="nickname  f-blue">'+obj.receiver+'</span>:<span class="nickname">'+obj.content+'</span></p>';
                                }else {
                                    // 没有接收人
                                    result += '<p><span class="nickname f-blue">'+obj.username+'</span>:<span class="nickname">'+obj.content+'</span></p><br />';
                                }
                            }



                            result += '</div>' +
                                '<div class="weui_cells weui_cells_form" style="display: none;">' +
                                '<div class="weui_cell weui_vcode">' +
                                '<div class="weui_cell_bd weui_cell_primary">' +
                                '<input class="weui_input" type="text" placeholder="评论">' +
                                '</div>' +
                                '<div class="weui_cell_ft">' +
                                '<a href="javascript:;" class="weui-vcode-btn bg-orange">发送</a>' +
                                '</div>' +
                                '</div>' +
                                '</div>' +
                                '</div>' +
                                '</div>';
						}
						// 如果没有数据
					} else {
						// 锁定
						me.lock();
						// 无数据
						me.noData();
					}
					// 为了测试，延迟1秒加载
					setTimeout(function() {
						$('.moments').append(result);
						var lazyloadImg = new LazyloadImg({
							el: '.weui-updown [data-img]', //匹配元素
							top: 50, //元素在顶部伸出长度触发加载机制
							right: 50, //元素在右边伸出长度触发加载机制
							bottom: 50, //元素在底部伸出长度触发加载机制
							left: 50, //元素在左边伸出长度触发加载机制
							qriginal: false, // true，自动将图片剪切成默认图片的宽高；false显示图片真实宽高
							load: function(el) {
								el.style.cssText += '-webkit-animation: fadeIn 01s ease 0.2s 1 both;animation: fadeIn 1s ease 0.2s 1 both;';
							},
							error: function(el) {

							}
						});
						//
						// 每次数据加载完，必须重置
						me.resetload();
					}, 1000);
				},
				error: function(xhr, type) {
					alert('上啦error!');
					// 即使加载出错，也得重置
					me.resetload();
				}
			});
		}
	});

});